'use strict';
const dayjs = require("dayjs");
module.exports = app => {
    const { INTEGER, DATE, STRING, TIME } = app.Sequelize;
    const Post = app.model.define(
      'Post',
          {
              id: {type: INTEGER, primaryKey: true, autoIncrement: true},
                user_id: INTEGER,
                category: STRING,
                category_id: INTEGER,
                post_category: STRING,
                post_category_id: INTEGER,
                name: STRING,
                day: INTEGER,
                amount: STRING,
                // 待遇
              welfare: STRING,
                desc: STRING,
                address: STRING,
              // 经纬度
              longitude: STRING,
              latitude: STRING,
                week: STRING,
              sort: INTEGER,
                start_time: TIME,
                end_time: TIME,
        start_date: {
                  type: DATE,
                  get() {
                      return dayjs(this.getDataValue('start_date')).format('YYYY-MM-DD');
                  }
              },
    end_date: {
                  type: DATE,
                  get() {
                      return dayjs(this.getDataValue('end_date')).format('YYYY-MM-DD');
                  }
              },
                  create_time: {
                      type: DATE,
                      get() {
                          return dayjs(this.getDataValue('create_time')).format('YYYY-MM-DD HH:mm:ss');
                      }
                  },
                  updated_time: {
                      type: DATE,
                      get() {
                          return dayjs(this.getDataValue('updated_time')).format('YYYY-MM-DD HH:mm:ss');
                      }
                  }
          },
          {
              tableName: 'post',
              timestamps: true, // 启用时间戳(createdAt, updatedAt)
              updatedAt: 'updated_time', // 不想要 updatedAt
              createdAt: 'create_time', // 想要  createdAt 但是希望名称叫做 create_time
          }
    );
    Post.associate = function() {
        // 关联user表
        app.model.Post.belongsTo(app.model.User, {foreignKey: 'user_id', targetKey: 'id', as: 'user'});
        // 关联postJoin表
        app.model.Post.hasMany(app.model.PostJoin, {foreignKey: 'post_id', targetKey: 'id', as: 'postJoinList'});
    }
    return Post;
};

